home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.2 / Arexx / mouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  4.8 KB  |  242 lines

  1. /*
  2.  * mouse.c v1.0 by Chris Ludwig
  3.  */
  4.  
  5. #include    <exec/types.h>
  6.  
  7. #include    <libraries/dos.h>
  8. #include    <libraries/dosextens.h>
  9.  
  10. #include    <devices/input.h>
  11. #include    <devices/inputevent.h>
  12.  
  13. #include    <proto/exec.h>
  14. #include    <proto/dos.h>
  15.  
  16. #include    <rexx/storage.h>
  17. #include    <rexx/rxslib.h>
  18.  
  19. #include    <stdio.h>
  20. #include    <string.h>
  21.  
  22. #include    "SimpleRexx.h"
  23.  
  24.  
  25. /* input.device stuff */
  26. struct MsgPort *inputdevport;
  27. struct InputEvent phony;
  28. struct IOStdReq *inputrequest;
  29. BYTE inputopenerror;
  30.  
  31. AREXXCONTEXT    RexxStuff;
  32.  
  33. void Quit(char whytext[],UBYTE level)
  34. {
  35.     if (!inputopenerror) CloseDevice((struct IORequest *) inputrequest);
  36.  
  37.     if (inputrequest) DeleteStdIO(inputrequest);
  38.  
  39.     if (inputdevport) DeletePort(inputdevport);
  40.  
  41.     FreeARexx(RexxStuff);
  42.  
  43.     printf("%s\n",whytext);
  44.     Exit(level);
  45. }
  46.  
  47.  
  48. /*
  49.  * Lattice control-c stop...
  50.  */
  51. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53.  
  54.  
  55. void click(UWORD code)
  56. {
  57. printf("in click()\n");
  58.  
  59.     phony.ie_NextEvent = NULL;
  60.     phony.ie_Class = IECLASS_RAWMOUSE;
  61.     phony.ie_TimeStamp.tv_secs = 0;
  62.     phony.ie_TimeStamp.tv_micro = 0;
  63.     phony.ie_Code = code;                    /* button down */
  64.     phony.ie_Qualifier = 0;
  65.     phony.ie_X = 0;
  66.     phony.ie_Y = 0;
  67.  
  68.     inputrequest -> io_Command = IND_WRITEEVENT;
  69.     inputrequest -> io_Flags = 0;
  70.     inputrequest -> io_Length = sizeof(struct InputEvent);
  71.     inputrequest -> io_Data = (APTR)&phony;
  72.  
  73.     DoIO(inputrequest);
  74.  
  75.  
  76.     phony.ie_NextEvent = NULL;
  77.     phony.ie_Class = IECLASS_RAWMOUSE;
  78.     phony.ie_TimeStamp.tv_secs = 0;
  79.     phony.ie_TimeStamp.tv_micro = 0;
  80.     phony.ie_Code = code | IECODE_UP_PREFIX;    /* button up */
  81.     phony.ie_Qualifier = 0;
  82.     phony.ie_X = 0;
  83.     phony.ie_Y = 0;
  84.  
  85.     inputrequest -> io_Command = IND_WRITEEVENT;
  86.     inputrequest -> io_Flags = 0;
  87.     inputrequest -> io_Length = sizeof(struct InputEvent);
  88.     inputrequest -> io_Data = (APTR)&phony;
  89.  
  90.     DoIO(inputrequest);
  91. }
  92.  
  93.  
  94. void move(LONG mousex, LONG mousey)
  95. {
  96.     /* send phony mouse message to input stream */
  97.  
  98.     phony.ie_NextEvent = NULL;
  99.     phony.ie_Class = IECLASS_POINTERPOS;
  100.     phony.ie_TimeStamp.tv_secs = 0;
  101.     phony.ie_TimeStamp.tv_micro = 0;
  102.     phony.ie_Code = 0;
  103.     phony.ie_Qualifier = 0;
  104.     phony.ie_X = mousex;
  105.     phony.ie_Y = mousey;
  106.  
  107.     inputrequest -> io_Command = IND_WRITEEVENT;
  108.     inputrequest -> io_Flags = 0;
  109.     inputrequest -> io_Length = sizeof(struct InputEvent);
  110.     inputrequest -> io_Data = (APTR)&phony;
  111.  
  112.     DoIO(inputrequest);
  113. }
  114.  
  115.  
  116. /*
  117.  * A *VERY* simple and simple-minded example of using the SimpleRexx.c code.
  118.  *
  119.  *
  120.  * Note: You will want to RUN this program or have another shell available such
  121.  *       that you can still have access to ARexx...
  122.  */
  123. void main(int argc,char *argv[])
  124. {
  125. short    loopflag=TRUE;
  126. ULONG    signals;
  127.  
  128. int x, y, length;
  129.  
  130.     if ((inputdevport=CreatePort(0,0)) == NULL)
  131.         Quit("Couldn't create a port for input.device",25);
  132.  
  133.  
  134.     if ((inputrequest=CreateStdIO(inputdevport)) == NULL)
  135.         Quit("Couldn't create request block for input device",25);
  136.  
  137.  
  138.     if ((inputopenerror=OpenDevice("input.device",0,inputrequest,0)) != 0)
  139.         Quit("Couldn't open input.device",25);
  140.  
  141.     /*
  142.      * Note that SimpleRexx is set up such that you do not
  143.      * need to check for an error to initialize your REXX port
  144.      * This is so your application could run without REXX...
  145.      */
  146.     RexxStuff=InitARexx("mouse","mouse");
  147.  
  148.     if (argc)
  149.     {
  150.         if (RexxStuff) printf("Send commands to port %s\n",ARexxName(RexxStuff));
  151.         else printf("ARexx is not available\n");
  152.     }
  153.  
  154.     while (loopflag)
  155.     {
  156.         signals=ARexxSignal(RexxStuff);
  157.  
  158.         if (signals)
  159.         {
  160.             struct    RexxMsg        *rmsg;
  161.  
  162.             signals=Wait(signals);
  163.  
  164.             /*
  165.              * Process the ARexx messages...
  166.              */
  167.             while (rmsg=GetARexxMsg(RexxStuff))
  168.             {
  169.             char    cBuf[24];
  170.             char    *nextchar;
  171.             char    *error=NULL;
  172.             char    *result=NULL;
  173.             long    errlevel=0;
  174.  
  175.                 nextchar=stptok(ARG0(rmsg),cBuf,24," ,");
  176.                 if (*nextchar) nextchar++;
  177.  
  178.                 if (!stricmp("CLICK",cBuf))
  179.                 {
  180.                     nextchar=stptok(nextchar,cBuf,24," ,");
  181.                     if (*nextchar) nextchar++;
  182.  
  183.                     if (!stricmp("LEFT",cBuf))
  184.                     {
  185.                         click(IECODE_LBUTTON);
  186.                     }
  187.                     else if (!stricmp("MIDDLE",cBuf))
  188.                     {
  189.                         click(IECODE_MBUTTON);
  190.                     }
  191.                     else if (!stricmp("RIGHT",cBuf))
  192.                     {
  193.                         click(IECODE_RBUTTON);
  194.                     }
  195.                     else
  196.                     {
  197.                         /* Default to left button */
  198.                         click(IECODE_LBUTTON);
  199.                     }
  200.                 }
  201.                 else if (!stricmp("MOVE",cBuf))
  202.                 {
  203.                     nextchar=stptok(nextchar,cBuf,24," ,");
  204.                     if (*nextchar) nextchar++;
  205.  
  206.                     length=stcd_i(cBuf,&x);
  207.  
  208.                     nextchar=stptok(nextchar,cBuf,24," ,");
  209.                     if (*nextchar) nextchar++;
  210.  
  211.                     length=stcd_i(cBuf,&y);
  212.  
  213.  
  214.                     move((LONG) x,(LONG) y);
  215.                 }
  216.                 else if (!stricmp("VERSION",cBuf))
  217.                 {
  218.                     result="mouse v1.0";
  219.                 }
  220.                 else if (!stricmp("QUIT",cBuf))
  221.                 {
  222.                     loopflag=FALSE;
  223.                 }
  224.                 else
  225.                 {
  226.                     error="Unknown command";
  227.                     errlevel=20;
  228.                 }
  229.  
  230.                 if (error)
  231.                 {
  232.                     SetARexxLastError(RexxStuff,rmsg,error);
  233.                 }
  234.                 ReplyARexxMsg(RexxStuff,rmsg,result,errlevel);
  235.             }
  236.  
  237.         }
  238.         else loopflag=FALSE;
  239.     }
  240.     Quit("QUIT command received.\n",0);
  241. }
  242.